//
//  HMBaseViewController.swift
//  WB25
//
//  Created by HM on 2017/6/29.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
/*
    新浪微博 一共有两个状态
        - 需要一个标识 -> BOOL -> true false
        - 登录状态
        - 访客状态
 */
/*
    - visitorView -> 闭包
    - 闭包 -> vc
    - vc -> visitorView
 */
class HMBaseViewController: UIViewController {
    
    // 判断是否登录
    let isLogin:Bool = HMOAuthViewModel.share.isLogin

    // 全局访客视图view
    var visitorView: HMVisitorView?
    
    override func loadView() {
        // 判断用户是否登录
        if !isLogin {
            setupVisitorView()
        }else{
            // 用户登录了
            view = tableView
        }
    }
    // 监听事件
    @objc private func loginClick(){
//        print("监听按钮点击弹出控制器gogogo")
        let oauthVc = HMOAuthViewController()
        let nav = HMNavigationController(rootViewController: oauthVc)
        // 模态形式弹出
        present(nav, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNav()

    }
    // 设置访客视图View
    private func setupVisitorView(){
        // 实例化HMVisitorView
        visitorView = HMVisitorView()
        // 02实例化闭包
        visitorView?.closure = {[weak self] in
            // 04执行闭包
            self?.loginClick()
        }
        view = visitorView
        setupNav()
    }
    
    // 设置导航
    private func setupNav(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: nil, title: "注册", target: self, action: #selector(loginClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imgName: nil, title: "登录", target: self, action: #selector(loginClick))
    }
    
    // 列表
    lazy var tableView: UITableView = {
        let view = UITableView()
        return view
    }()

}
